package com.bestcem.xm.ticket.service.manager.role;

import com.bestcem.xm.ticket.enums.FollowerFilterEnum;
import com.bestcem.xm.ticket.grpc.client.TicketUserGrpcService;
import com.bestcem.xm.ticket.grpc.client.dto.user.UserDTO;
import com.bestcem.xm.ticket.task.TicketTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 工单规则 跟进人/审核人 操作工具类
 *
 * @author XiaoYunSong <ys.xiao@idiaoyan.com>
 * @version v1.0
 * @date 2021/8/18 17:17
 */
@Slf4j
@Component
public class TicketRoleManager {
    @Resource
    private TicketTask ticketTask;

    @Resource
    private TicketUserGrpcService ticketUserGrpcService;

    /**
     * 根据不同 跟进人/审核人 选择权限，判断是否有用户
     *
     * @param self 跟进者/审核人 信息
     * @return 是否有对应用户
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2021/8/18 17:21
     */
    public boolean hasUser(BaseTicketRole self, String region) {
        List<UserDTO> userDTOS = ticketUserGrpcService.listUsersByRole(self.getRoleId(), region, self.getRegion());
        if (log.isInfoEnabled()) {
            log.info("[ticket]: 判断跟进是否有用户,用户list:{},roleId：{},groupId:{},levelId:{}",
                    userDTOS, self.getRoleId(), region, self.getRegion());
        }
        return !CollectionUtils.isEmpty(userDTOS);
    }


    /**
     * 根据不同跟进人选择权限，判断是否有用户
     *
     * @param self    跟进者信息
     * @param groupId Ticket-region == groupId
     * @return 是否有对应用户
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2021/8/18 17:20
     */
    public boolean hasUser(TicketFollower self, String groupId) {
        if (FollowerFilterEnum.REGION.equals(self.getFollowerFilter())) {
            List<UserDTO> userDTOS = ticketUserGrpcService.listUsersByRole(self.getRoleId(), groupId, self.getRegion());
            if (log.isInfoEnabled()) {
                log.info("[ticket]: 判断跟进是否有用户,followerId:{},用户list:{},roleId：{},groupId:{},levelId:{}",
                        self.getFollowerId(), userDTOS, self.getRoleId(), groupId, self.getRegion());
            }
            return !CollectionUtils.isEmpty(userDTOS);
        }
        // return bool(await get_user_is_headquarters([self.follower_id]))
        List<UserDTO> userDTOList = ticketTask.getUserIsHeadquarters(Arrays.asList(self.getFollowerId()));
        if (log.isInfoEnabled()) {
            log.info("[ticket]: 判断跟进是否有用户,followerId:{},用户list:{}", self.getFollowerId(), userDTOList);
        }
        return !CollectionUtils.isEmpty(userDTOList);
    }


    /**
     * 根据不同审核人选择权限，判断是否有用户
     *
     * @param self 审核人信息
     * @return 是否有对应用户
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2021/8/18 17:20
     */
    public boolean hasUser(TicketAuditor self) {
        return false;
    }
}
